/**
 * 
 */
package com.mywind.controller.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.mywind.constant.Constant;
import com.mywind.entity.core.ResponseObj;
import com.mywind.entity.core.User;
import com.mywind.service.core.IUserServ;

/**
 * @author A01162
 * 
 */
@Controller
@SessionAttributes("mysession")
@RequestMapping(value = "role")
public class RoleController implements Constant {

	@Autowired
	private IUserServ userServ;

	/**
	 * 加载用户列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/datagrid.do")
	@ResponseBody
	public ResponseObj userDataGrid() {
		ResponseObj ro = new ResponseObj();
		List<User> datagrid = userServ.findAllUsers();

		ro.setResBody(datagrid);
		ro.setStatus(SUC);
		return ro;
	}

	/**
	 * 页面加载
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toRole.do", method = RequestMethod.GET)
	public String loadPage() {
		return "underConstruction";
	}

	@RequestMapping(value = "/editUser.do", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObj editUser(User userArg,
			@RequestParam("oper") String oper, ModelMap modelMap) {
		ResponseObj rb = new ResponseObj();
		User mysession = (User) modelMap.get("mysession");
		User user = userArg;
		if (EDIT.equalsIgnoreCase(oper)) {
			user.setLastModifyTime(new Date());
			user.setLastModifyUser(mysession.getAccount());
			userServ.updateUser(user);
			user = userServ.findUserById(user.getId());
			rb.setStatus(SUC);
			rb.setMessage("修改用户成功");
			rb.setResBody(user);
			return rb;
		} else if (ADD.equalsIgnoreCase(oper)) {
			if (userServ.findUserByAccount(user.getAccount()) != null) {
				rb.setStatus(ERR);
				rb.setMessage("账号已存在");
				return rb;
			} else {
				user.setPassword("");
				user.setCreateTime(new Date());
				user.setCreateUser(mysession.getAccount());
				userServ.insertUser(user);

				rb.setStatus(SUC);
				rb.setMessage("新增用户成功");
				rb.setResBody(user);
				return rb;
			}

		} else if (DEL.equalsIgnoreCase(oper)) {
			userServ.deleteUserById(new Date(), mysession.getAccount(),user.getId());

			rb.setStatus(SUC);
			rb.setMessage("删除用户成功");
			return rb;
		}
		return null;
	}
	
	@RequestMapping(value = "/delUsersByIds.do", method = RequestMethod.POST)
	@ResponseBody
	public ResponseObj deleteUsersByIds(@RequestParam("ids") String ids, ModelMap modelMap){
		ResponseObj rb = new ResponseObj();
		User mysession = (User) modelMap.get("mysession");
		List<Integer> idsList = new ArrayList<Integer>();
		String[] idsArr = ids.split(",");
		if (idsArr.length > 0) {
			for (int i = 0; i < idsArr.length; i++) {
				idsList.add(Integer.parseInt(idsArr[i]));
			}
			userServ.deleteUsersByIds(new Date(),mysession.getAccount(),idsList);
			rb.setStatus(SUC);
			rb.setMessage("删除用户成功");
			return rb;
		}
		rb.setStatus(ERR);
		rb.setMessage("请选择要删除的数据");
		return rb;
	}
}
